package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.merchant.ActivityFaqColumnEntity;
import com.dragon.modules.yuegou.entity.merchant.ActivityFaqEntity;
import com.dragon.modules.yuegou.mapper.merchant.ActivityFaqColumnMapper;
import com.dragon.modules.yuegou.model.merchant.ActivityFaq;
import com.dragon.modules.yuegou.model.merchant.ActivityFaqColumn;
import com.dragon.modules.yuegou.query.merchant.ActivityFaqColumnQuery;
import com.dragon.modules.yuegou.query.merchant.ActivityFaqQuery;
import com.dragon.modules.yuegou.service.merchant.ActivityFaqColumnService;
import com.dragon.modules.yuegou.service.merchant.ActivityFaqService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 咨询栏目 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-23 10:30:38
 * @version 2.0
 */
@Service("merActivityFaqColumnService")
public class ActivityFaqColumnServiceImpl
		extends ServiceImpl<ActivityFaqColumnMapper, ActivityFaqColumnEntity>
		implements ActivityFaqColumnService {

	@Autowired
	private ActivityFaqService activityFaqService;
	
	@Override
	public List<ActivityFaqColumn> columnList() {
		ActivityFaqColumnQuery query = new ActivityFaqColumnQuery();
		List<Integer> vals = new ArrayList<>();
		vals.add(0);
		query.sortNo().notIn(vals);
		QueryWrapper<ActivityFaqColumnEntity> queryWrapper = query.toQueryWrapper();
		queryWrapper.orderByAsc(ActivityFaqColumnEntity.COL_SORT_NO);
		return list(queryWrapper).stream().map(ActivityFaqColumn::new).collect(Collectors.toList());
	}
	
	@Override
	@Transactional
	public void column(List<ActivityFaqColumn> columns,String busdId) {
		//添加商圈栏目
		Integer i = 1;
		for (ActivityFaqColumn column : columns) {
			column.setSortNo(i);
			column.setBusdId(busdId);
			if (StringUtils.isEmpty(column.getId())) {
				save(column);
			}else {
				updateById(column);
			}
			i++;
		}
	}

	@Override
	public List<ActivityFaqColumn> faqColumnList(String busdId) {
		ActivityFaqColumnQuery query = new ActivityFaqColumnQuery();
		query.setBusdId(busdId);
		QueryWrapper<ActivityFaqColumnEntity> queryWrapper = query.toQueryWrapper();
		queryWrapper.orderByAsc(ActivityFaqColumnEntity.COL_SORT_NO);
		List<ActivityFaqColumnEntity> list = list(queryWrapper);

		if (CollectionUtils.isNotEmpty(list)){
			List<String> idList = list.stream().map(ActivityFaqColumnEntity::getId).collect(Collectors.toList());
			ActivityFaqQuery activityFaqQuery = new ActivityFaqQuery();
			activityFaqQuery.columnId().in(idList);
			QueryWrapper<ActivityFaqEntity> wrapper = activityFaqQuery.toQueryWrapper();
			wrapper.orderByAsc(ActivityFaqEntity.COL_SORT_NO);
			wrapper.orderByDesc(ActivityFaqEntity.COL_CREATED_TIME);
			List<ActivityFaqEntity> activityFaqs = activityFaqService.list(wrapper);
			Map<String,List<ActivityFaqEntity>> activityFaqMap = activityFaqs.stream().collect(Collectors.groupingBy(ActivityFaqEntity::getColumnId));

			return list.stream().map(activityFaqColumnEntity -> {
				ActivityFaqColumn activityFaqColumn = new ActivityFaqColumn(activityFaqColumnEntity);
				List<ActivityFaqEntity> activityFaqEntities = activityFaqMap.get(activityFaqColumn.getId());
				if (CollectionUtils.isNotEmpty(activityFaqEntities)){
					activityFaqColumn.setActivityFaqs(activityFaqEntities.stream().map(ActivityFaq::new).collect(Collectors.toList()));
				}
				return activityFaqColumn;
			}).collect(Collectors.toList());
		}

		return new ArrayList<>();
	}

	@Override
	@Transactional
	public void advisory(String id,String phone) {
		Check.notEmpty(phone,"咨询电话不能为空");
		
		ActivityFaqColumn activityFaqColumn = new ActivityFaqColumn();
		
		if (StringUtils.isEmpty(id)) {
			//添加栏目-咨询电话
			activityFaqColumn.setColumnName("咨询电话");
			activityFaqColumn.setSortNo(0);
			save(activityFaqColumn);
			
			//添加咨询问题
			ActivityFaq activityFaq = new ActivityFaq();
			activityFaq.setTitle(phone);
			activityFaq.setColumnId(activityFaqColumn.getId());
			activityFaqService.saveFaq(activityFaq);
		}else {
			ActivityFaqQuery query = new ActivityFaqQuery();
			query.setColumnId(id);
			List<ActivityFaqEntity> activityFaqEntityList = activityFaqService.list(query.toQueryWrapper());
			if (CollectionUtils.isNotEmpty(activityFaqEntityList)){
				ActivityFaqEntity activityFaq =activityFaqEntityList.get(0);
				activityFaq.setTitle(phone);
				activityFaqService.updateById(activityFaq);
			}
		}
	}

	@Override
	public ActivityFaqColumn getAdvisory() {
		//查询咨询电话
		ActivityFaqColumnQuery query = new ActivityFaqColumnQuery();
		query.setSortNo(0);
		List<ActivityFaqColumnEntity> entities = list(query.toQueryWrapper());
		if (CollectionUtils.isNotEmpty(entities)) {
			ActivityFaqColumn one = new ActivityFaqColumn(entities.get(0));
			ActivityFaqQuery activityFaqQuery = new ActivityFaqQuery();
			activityFaqQuery.setColumnId(one.getId());
			List<ActivityFaqEntity> list = activityFaqService.list(activityFaqQuery.toQueryWrapper());
			one.setActivityFaqs(list.stream().map(ActivityFaq::new).collect(Collectors.toList()));
			return one;
		}else {
			return new ActivityFaqColumn();
		}
	}

}